/* $Id: GUIUtils.java 39 2009-07-24 22:48:06Z h_m_r@gmx.de $ */
package util;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class GUIUtils {
	public static JPanel createCaptionPanel(String text) {
		JPanel caption = new JPanel();
		JLabel label = new JLabel(text);
		caption.setLayout(new BoxLayout(caption, BoxLayout.X_AXIS));
		caption.add(label);
		caption.add(Box.createHorizontalGlue());
		return caption;
	}

	public static TitledBorder createTitledBorder(String text) {
		return new TitledBorder(new LineBorder(Color.DARK_GRAY, 1, true), text);
	}

	public static JPanel createXBoxLayoutPanel() {
		JPanel result = new JPanel();
		result.setLayout(new BoxLayout(result, BoxLayout.X_AXIS));
		return result;
	}

	public static JPanel createYBoxLayoutPanel() {
		JPanel result = new JPanel();
		result.setLayout(new BoxLayout(result, BoxLayout.Y_AXIS));
		return result;
	}

	public static JPanel createTitledPanelY(String title) {
		JPanel result = createYBoxLayoutPanel();
		result.setBorder(createTitledBorder(title));
		return result;
	}

	public static JLabel createFixedWidthLabel(String text, int width) {
		JLabel result = new JLabel(text);
		// result.setMinimumSize(new Dimension(width, Integer.MIN_VALUE));
		// result.setMaximumSize(new Dimension(width, Integer.MAX_VALUE));
		return result;
	}

	public static JPanel createTitledPanelX(String title) {
		JPanel result = createXBoxLayoutPanel();
		result.setBorder(createTitledBorder(title));
		return result;
	}

	public static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = ClassLoader.getSystemResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			return null;
		}
	}

	public static Image createImage(String path) {
		java.net.URL imgURL = ClassLoader.getSystemResource(path);
		if (imgURL != null) {
			return Toolkit.getDefaultToolkit().getImage(imgURL);
		} else {
			return null;
		}
	}

	public static JTextField createTextField(int cols) {
		JTextField result = new JTextField(cols);
		result.setPreferredSize(result.getMinimumSize());
		return result;
	}
	public static JCheckBox createCheckBox() {
		JCheckBox result = new JCheckBox();
		result.setPreferredSize(result.getMinimumSize());
		return result;
	}

}
